package com.hxg.bl.controller;

import com.github.pagehelper.PageInfo;
import com.hxg.bl.bean.AppointmentConfBean;
import com.hxg.bl.bean.AppointmentPeriodTimeBean;
import com.hxg.bl.dao.jpa.AppointmentConfBeanJpa;
import com.hxg.bl.service.AppointmentConfService;
import com.hxg.bl.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/appointment/conf")
public class AppointmentConfController {

    @Autowired
    private AppointmentConfBeanJpa appointmentConfBeanJpa;

    @Autowired
    private AppointmentConfService appointmentConfService;

    @RequestMapping("/listAppointmentConf")
    public String listAppointmentConf() {
        return "bl/appointment/appointment_conf_list";
    }

    @RequestMapping("/listAppointmentConfByParams")
    @ResponseBody
    public PageInfo<AppointmentConfBean> listAppointmentConfByParams(
            @RequestParam(value = "pn", defaultValue = "1") Integer pn,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            SearchVo searchVo
    ) {
        return appointmentConfService.listAppointmentConfByParams(pn, pageSize, searchVo);
    }

    @RequestMapping("/getTimeRangeByIdList")
    @ResponseBody
    public Map<String, Object> getTimeRangeByIdList(
            @RequestParam("idList") String idList
    ) {
        Map<String, Object> jsonMap = new HashMap<>();

        try {
            List<Map<String, Object>> timeRangeList = appointmentConfService.getTimeRangeByIdList(idList);

            jsonMap.put("timeRangeList", timeRangeList);
            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }

    @RequestMapping("/addOrEditAppointmentConf")
    public String addOrEditAppointmentConf(
            Map<String, Object> map,
            @RequestParam(value = "id", defaultValue = "") Long id
    ) {
        AppointmentConfBean appointmentConfBean = new AppointmentConfBean();
        if (null != id && id > 0) {
            appointmentConfBean = appointmentConfBeanJpa.getOne(id);
        } else {
            List<AppointmentPeriodTimeBean> appointmentPeriodTimeBeanList = Arrays.asList(
                    new AppointmentPeriodTimeBean("9:30", "17:00", 10)
            );

            System.out.println(appointmentPeriodTimeBeanList);
            appointmentConfBean.setAppointmentPeriodTimeBeanList(appointmentPeriodTimeBeanList);
        }

        map.put("appointmentConfBean", appointmentConfBean);

        return "bl/appointment/appointment_conf_add_or_edit";
    }

    @RequestMapping("/saveAndFlushAppointmentConf")
    @ResponseBody
    public Map<String, Object> saveAndFlushAppointmentConf(
            AppointmentConfBean appointmentConfBean
    ) {
        Map<String, Object> jsonMap = new HashMap<>();

        try {
            appointmentConfService.saveAndFlushAppointmentConf(appointmentConfBean);
            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }

    @RequestMapping("/batchDelAppointmentConf")
    @ResponseBody
    public Map<String, Object> batchDelAppointmentConf(
            @RequestParam("idList") String idList
    ) {
        Map<String, Object> jsonMap = new HashMap<>();

        try {
            appointmentConfService.batchDelAppointmentConf(
                    Arrays.asList(idList.split(",")).stream()
                            .map(s -> Long.parseLong(s))
                            .collect(Collectors.toList())
            );
            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }

    @RequestMapping("/active")
    @ResponseBody
    public Map<String, Object> active(
            @RequestParam("id") Long id,
            @RequestParam("active") Integer active
    ) {

        Map<String, Object> jsonMap = new HashMap<>();

        try {
            AppointmentConfBean appointmentConfBean = appointmentConfBeanJpa.getOne(id);
            appointmentConfBean.setActive(active);
            appointmentConfBeanJpa.saveAndFlush(appointmentConfBean);
            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }

}
